import Vue from 'vue'
import Vuex from 'vuex'
import { Notification } from 'element-ui';
import Router from '../router/index'
let URLAddress = "api.darenexpress.com/"
// let URLAddress = "localhost:5656/"
let IsHttps = true
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    baseURL: 'http' + (IsHttps ? 's' : '') + '://' + URLAddress,//'http://c5h7f6lzou.cdhttp.cn/erbapi/',
    baseWebSocket: 'ws' + (IsHttps ? 's' : '') + '://' + URLAddress,
    hasLogin: false,
    userName: "",
    token: "",
    limitsAuthority: { isDefaultRole: false, menuIndexs: [], menuDict: {}, roleMenuFunctionInfos: [] },

    msgList: [],
    sidebarCollapse: false,
  },
  mutations: {
    setSidebarCollapse(state, collapse) {
      state.sidebarCollapse = collapse
    },
    pushMsg(state, data) {
      let defalut = {
        duration: 30000,
        offset: 90,
        title: data.title,
        message: data.message,
        onClick: (e) => {
          // Router.push({
          //   name: 'packageApplyOutTable', params: {
          //     func: () => {
          //       console.log("TEST")
          //     }
          //   }
          // })
        }
        // onClose: (e) => {
        //   for (let i = 0; i < state.msgList.length; i++) {
        //     if (state.msgList[i].id === e.id) {
        //       state.msgList.splice(i, 1)
        //       break
        //     }
        //   }
        // }
      }
      let Notice = Notification.info(defalut);
      data.Notice = Notice
      state.msgList.push(data)
    },
    removeMsgIndex(state, index) {
      state.msgList[index].Notice.close()
      state.msgList.splice(index, 1)
    },
    removeMsgAll(state) {
      state.msgList.map(it => {
        it.Notice.close()
      })
      state.msgList = []
    },
    userLogin(state, userInfo) {
      state.userName = userInfo.userName;
      state.hasLogin = true;
      state.token = userInfo.userData.token;
      let { isDefaultRole, menuIndexs, roleMenuFunctionInfos } = userInfo.userData
      let menuDict = {}
      if (menuIndexs instanceof Array) {
        menuIndexs.map(inde => {
          menuDict[String(inde)] = true
        })
      }
      state.limitsAuthority = { isDefaultRole, menuIndexs, menuDict, roleMenuFunctionInfos }
      localStorage.setItem('UserInfo', JSON.stringify(userInfo));
    },
    userLogout(state) {
      state.userName = "";
      state.hasLogin = false;
      state.token = null;
      state.limitsAuthority = { isDefaultRole: false, menuIndexs: [], menuDict: {}, roleMenuFunctionInfos: [] }
      localStorage.removeItem('UserInfo');
      state.msgList.map(it => {
        it.Notice.close()
      })
      state.msgList = []
    },
    userGetCache(state) {
      console.log("userGetCache")
      let userInfo = localStorage.getItem('UserInfo');
      if (userInfo) {
        userInfo = JSON.parse(userInfo)
        state.userName = userInfo.userName;
        state.hasLogin = true;
        state.token = userInfo.userData.token;
        let { isDefaultRole, menuIndexs, roleMenuFunctionInfos } = userInfo.userData
        let menuDict = {}
        if (menuIndexs instanceof Array) {
          menuIndexs.map(inde => {
            menuDict[String(inde)] = true
          })
        }
        state.limitsAuthority = { isDefaultRole, menuIndexs, menuDict, roleMenuFunctionInfos }
      } else {
        state.userName = "";
        state.hasLogin = false;
        state.token = null;
        state.limitsAuthority = { isDefaultRole: false, menuIndexs: [], menuDict: {}, roleMenuFunctionInfos: [] }
      }
    },
  }
})

export default store